package com.lushuan.ch11_springboot_test.demo.threadpool;

import java.util.concurrent.*;

public class ThreadPoolDemo {

    public static void main(String[] args) {
        ExecutorService executorService1 = Executors.newCachedThreadPool(); // 最快
        ExecutorService executorService2 = Executors.newFixedThreadPool(10); // 中等
        ExecutorService executorService3 = Executors.newSingleThreadExecutor(); // 最慢
        // 自定义
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,
                20,
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10));

        for (int i = 0; i < 100; i++) {
            threadPoolExecutor.execute(new MyTask(i));
        }

    }

}


class MyTask implements Runnable {

    int i;
    public MyTask(int i) {
        this.i = i;
    }
    @Override

    public void run() {
        System.out.println(Thread.currentThread().getName()+'-'+i);
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
